﻿@using Fierce.MvcExtend.Controls.Manage

<div class="easyui-layout" data-options="fit:true">

    <div data-options="region:'north',border:false,height:112">
        <div class="document-name">@ViewBag.Work.Menu.Name</div>
        <div class="document-toolbar">@Easyui.Toolbar(ViewBag.Work)<input id="usearch" /></div>
    </div>

    <div data-options="region:'center',border:false">
        <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'center'">
                <table id="ugrid"></table>
            </div>
            <div data-options="region:'east',width:278">
                <div class="easyui-layout" data-options="fit:true">
                    <div data-options="region:'north',border:false,height:222,href:'@Easyui.Embed(ViewBag.Work.Menu.ID, "userdepartmentpost")'"></div>
                    <div data-options="region:'center',href:'@Easyui.Embed(ViewBag.Work.Menu.ID, "userrole")'" style="border-width:1px 0 0;"></div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        //表格初始化
        var gridInit = function () {
            $("#ugrid").datagrid({
                url: "/basic/user/grid",
                nowrap: false,
                rownumbers: true,
                border: false,
                fit: true,
                singleSelect: true,
                idField: "ID",
                toolbar: "#utools",
                pageSize: 20,
                pagination: true,
                columns: [[
                    { field: "UserName", title: "账号", width: 140, align: "center" },
                    { field: "Name", title: "姓名", width: 80, align: "center" },
                    { field: "Mail", title: "邮箱", width: 180, align: "center" },
                    { field: "Remark", title: "描述", width: 200, align: "center" },
                    {
                        field: "IsEnabled", title: "启用", width: 50, align: "center", sortable: true,
                        formatter: function (value, row, index) { return manage.iconYesOrNo(value); }
                    }
                ]],
                onSelect: function (index, row) {
                    $("#udpgrid").datagrid("unselectAll");
                    $("#udpgrid").datagrid("load", { userid: row.ID });
                    $("#urgrid").datagrid("unselectAll");
                    $("#urgrid").datagrid("load", { userid: row.ID });
                }
            });
        }();
        //搜索初始化
        var searchInit = function () {
            $("#usearch").searchbox({
                prompt: "请输入账号或姓名查询",
                width: 200,
                searcher: function (value) {
                    $("#udpgrid").datagrid("unselectAll");
                    $("#udpgrid").datagrid("loadData", []);
                    delete $("#udpgrid").datagrid("options").queryParams;
                    $("#urgrid").datagrid("unselectAll");
                    $("#urgrid").datagrid("loadData", []);
                    delete $("#urgrid").datagrid("options").queryParams;
                    $("#ugrid").datagrid("unselectAll");
                    $("#ugrid").datagrid("load", { search: value });
                }
            });
        }();
        //添加
        var insert = function () {
            mutual("添加", null);
        };
        //修改
        var update = function () {
            var row = $("#ugrid").datagrid("getSelected");
            if (row) {
                mutual("修改", row.ID);
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //删除
        var remove = function () {
            var row = $("#ugrid").datagrid("getSelected");
            if (row) {
                manage.confirm("确认删除?", function (del) {
                    if (del) {
                        manage.ajax({
                            url: "/basic/user/remove",
                            type: "post", data: { id: row.ID },
                            success: function (result) {
                                manage.slide(result.message);
                                if (result.success) { reload(); }
                            }
                        });
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //实体操作
        var mutual = function (title, id) {
            var href = "/basic/user/page?" + $.param({ id: id });
            var d = manage.dialog({
                title: title, href: href, width: 420,
                bodyCls: "theme-container",
                handler: function () {
                    manage.form({
                        dialog: d, formid: "#uform",
                        url: "/basic/user/save",
                        success: function (result) { reload(); }
                    });
                },
                onLoad: function () { formInit(id); },
                onClose: function () { uploader.destroy(); }
            });
        };
        //上传组件
        var uploader = undefined;
        //弹窗初始化
        var formInit = function (id) {
            if (id) {
                $("#password").remove();
                $("#username").textbox("disable");
            }
            uploader = manage.uploadImgCutInit();
        };
        //启用
        var enable = function () {
            enableOrDisable(true);
        };
        //禁用
        var disable = function () {
            enableOrDisable(false);
        };
        //启用禁用
        var enableOrDisable = function (bool) {
            var row = $("#ugrid").datagrid("getSelected");
            if (row) {
                manage.ajax({
                    url: "/basic/user/enableordisable",
                    type: "post", data: { id: row.ID, bit: bool },
                    success: function (result) {
                        manage.slide(result.message);
                        if (result.success) {
                            $("#ugrid").datagrid("reload");
                        }
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //刷新列表
        var reload = function () {
            $("#udpgrid").datagrid("unselectAll");
            $("#udpgrid").datagrid("loadData", []);
            delete $("#udpgrid").datagrid("options").queryParams;
            $("#urgrid").datagrid("unselectAll");
            $("#urgrid").datagrid("loadData", []);
            delete $("#urgrid").datagrid("options").queryParams;
            $("#ugrid").datagrid("unselectAll");
            $("#ugrid").datagrid("reload");
        };
        //重置密码
        var reset = function () {
            var row = $("#ugrid").datagrid("getSelected");
            if (row) {
                var d = manage.dialog({
                    title: "重置密码", width: 420,
                    bodyCls: "theme-container",
                    href: "/basic/user/resetpwd?" + $.param({ id: row.ID }),
                    handler: function () {
                        manage.form({
                            dialog: d, formid: "#resetpwd",
                            url: "/basic/user/setresetpwd",
                            success: function (result) { }
                        });
                    },
                    onLoad: function () { }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //按钮事件
        manage.buttonInit("basicuser", function (f) { return eval(f); });
    });
</script>